package com.jgp.rmi.client;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-05-08
 */
public class MyRmiProxyFactory<T> implements FactoryBean<T>, InitializingBean {
    
    private Class<T> interfaceClass;
    private String url = "localhost:1099";
    
    public MyRmiProxyFactory() {
    }
    
    public MyRmiProxyFactory(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }
    
    public MyRmiProxyFactory(String url,Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
        this.url = url;
    }
    
    public T createBean(){
        RmiProxyFactoryBean bean = new RmiProxyFactoryBean();
        bean.setServiceUrl("rmi://"+url+"/"+interfaceClass.getSimpleName());
        bean.setServiceInterface(interfaceClass);
        bean.afterPropertiesSet();
        return (T)bean.getObject();
    }
    
    @Override
    public T getObject() throws Exception {
        return createBean();
    }
    
    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }
    
    @Override
    public boolean isSingleton() {
        return false;
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterpropertiesset");
    }
}
